const { Category } = require("../database/sequlize");

// 获取所有分类
exports.getAllCategories = async () => {
  return Category.findAll();
};

// 获取单个分类通过id
exports.getCategoryById = async (categoryId) => {
  return Category.findByPk(categoryId);
};

// 创建新的分类
exports.createCategory = async (categoryData) => {
  return Category.create(categoryData);
};

// 更新分类
exports.updateCategory = async (categoryId, categoryData) => {
  const category = await Category.findByPk(categoryId);
  if (!category) {
    throw new Error("Category not found");
  }
  return category.update(categoryData);
};

// 删除分类
exports.deleteCategory = async (categoryId) => {
  const category = await Category.findByPk(categoryId);
  if (!category) {
    throw new Error("Category not found");
  }
  return category.destroy();
};
